package com.itqian.common.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author shishuqian
 * date 2021/12/16
 * time 20:17
 **/
@Configuration
public class RedisConf {

    @Bean
    public RedisTemplate<String, Object> myRedisTemplate(RedisConnectionFactory redisConnectionFactory){
        //常用<String,Object>
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //设置默认序列化方式 可以是：Jackson2JsonRedisSerializer GenericJackson2JsonRedisSerializer
        redisTemplate.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
        //声明字符串序列化方式
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        //设置key以字符串序列化
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);

        return redisTemplate;


    }

}
